package com.simple.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.annotation.Log;
import com.simple.common.core.controller.BaseController;
import com.simple.common.core.domain.AjaxResult;
import com.simple.common.enums.BusinessType;
import com.simple.project.domain.BlogTag;
import com.simple.project.service.IBlogTagService;
import com.simple.common.utils.poi.ExcelUtil;
import com.simple.common.core.page.TableDataInfo;

/**
 * 博客标签关联Controller
 * 
 * @author simple
 * @date 2020-12-01
 */
@RestController
@RequestMapping("/project/blogTag" )
public class BlogTagController extends BaseController {

    @Autowired
    private IBlogTagService iBlogTagService;

    /**
     * 查询博客标签关联列表
     */
    @PreAuthorize("@ss.hasPermi('project:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogTag blogTag)
    {
        startPage();
        LambdaQueryWrapper<BlogTag> lqw = Wrappers.lambdaQuery(blogTag);
        if (blogTag.getBlogId() != null){
            lqw.eq(BlogTag::getBlogId ,blogTag.getBlogId());
        }
        if (blogTag.getTagId() != null){
            lqw.eq(BlogTag::getTagId ,blogTag.getTagId());
        }
        if (StringUtils.isNotBlank(blogTag.getTagName())){
            lqw.like(BlogTag::getTagName ,blogTag.getTagName());
        }
        if (blogTag.getIsDeleted() != null){
            lqw.eq(BlogTag::getIsDeleted ,blogTag.getIsDeleted());
        }
        List<BlogTag> list = iBlogTagService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出博客标签关联列表
     */
    @PreAuthorize("@ss.hasPermi('project:tag:export')" )
    @Log(title = "博客标签关联" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BlogTag blogTag) {
        LambdaQueryWrapper<BlogTag> lqw = new LambdaQueryWrapper<BlogTag>(blogTag);
        List<BlogTag> list = iBlogTagService.list(lqw);
        ExcelUtil<BlogTag> util = new ExcelUtil<BlogTag>(BlogTag. class);
        return util.exportExcel(list, "tag" );
    }

    /**
     * 获取博客标签关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:tag:query')" )
    @GetMapping(value = "/{blogTagId}" )
    public AjaxResult getInfo(@PathVariable("blogTagId" ) Long blogTagId) {
        return AjaxResult.success(iBlogTagService.getById(blogTagId));
    }

    /**
     * 新增博客标签关联
     */
    @PreAuthorize("@ss.hasPermi('project:tag:add')" )
    @Log(title = "博客标签关联" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogTag blogTag) {
        return toAjax(iBlogTagService.save(blogTag) ? 1 : 0);
    }

    /**
     * 修改博客标签关联
     */
    @PreAuthorize("@ss.hasPermi('project:tag:edit')" )
    @Log(title = "博客标签关联" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogTag blogTag) {
        return toAjax(iBlogTagService.updateById(blogTag) ? 1 : 0);
    }

    /**
     * 删除博客标签关联
     */
    @PreAuthorize("@ss.hasPermi('project:tag:remove')" )
    @Log(title = "博客标签关联" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{blogTagIds}" )
    public AjaxResult remove(@PathVariable Long[] blogTagIds) {
        return toAjax(iBlogTagService.removeByIds(Arrays.asList(blogTagIds)) ? 1 : 0);
    }
}
